package fr.emse.tatiana.commands;



import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.MessageDialogWithToggle;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.window.Window;
import org.eclipse.ui.IWorkbenchWindow;


import fr.emse.tatiana.CorpusNavigationView;


/**
 * Action to delete a corpus - only deletes it from the project list - files remain intact
 * 
 * @author dyke
 *
 */
public class DeleteCorpusAction extends Action {

	private final IWorkbenchWindow window;
	private final String viewId;

	private static DeleteCorpusAction instance;

	public static DeleteCorpusAction getInstance() {
		return instance;
	}

	public DeleteCorpusAction(IWorkbenchWindow window, String label, String id) {
		this.window = window;
		this.viewId = id;
		setText(label);
		// The id is used to refer to the action in a menu or toolbar
		setId(ICommandIds.CMD_DELETE_CORPUS);
		// Associate the action with a pre-defined command, to allow key bindings.
		//setActionDefinitionId(ICommandIds.CMD_DELETE_CORPUS);
		setImageDescriptor(fr.emse.tatiana.Activator.getImageDescriptor("/icons/delete_obj.gif"));
		this.setEnabled(false);
		instance = this;
	}


	@Override
	public void run() {
		if(window != null) {	
			IProject project = ((CorpusNavigationView) window.getActivePage().findView(this.viewId)).getSelectedProject();
			if (project == null) {
				return;
			}
			MessageDialogWithToggle dialog=MessageDialogWithToggle.openOkCancelConfirm(window.getShell(), "Delete Project","This will delete the project in Tatiana. Do you want to delete this project?","This will delete the corpus permanently",false,null, "BUG_USER_KEY");
			if(dialog.getReturnCode()==Window.CANCEL){
				return;
			}
			else{
				if(dialog.getToggleState()){
					try {
						project.delete(true, true, null);
					} catch (CoreException e) {
						// won't happen
					}
				}
				else{
					try {
						project.delete(false, true, null);
					} catch (CoreException e) {
						// won't happen
					}
				}
			}
		}
	}
}

		
